package com.bestcem.xm.om.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.common.core.enums.ReqErrorCodeEnum;
import com.bestcem.xm.common.core.enums.ResourceErrorCodeEnum;
import com.bestcem.xm.common.core.enums.ValidationErrorCodeEnum;
import com.bestcem.xm.om.controller.param.CreateOmRoleParam;
import com.bestcem.xm.om.controller.param.UpdateOmRoleParam;
import com.bestcem.xm.om.controller.vo.OmRoleVO;
import com.bestcem.xm.om.controller.vo.SimpleIdVO;
import com.bestcem.xm.om.dao.OmRoleDao;
import com.bestcem.xm.om.dao.OmUserDao;
import com.bestcem.xm.om.entity.OmRole;
import com.bestcem.xm.om.entity.OmUser;
import com.bestcem.xm.om.enums.OmRoleTypeEnum;
import com.bestcem.xm.om.grpc.service.converter.OmUserConverter;
import com.bestcem.xm.om.service.OmRoleService;
import com.bestcem.xm.om.service.dto.common.UserPageDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author zq.ding <zq.ding@idiaoyan.com>
 * @version v1.0
 * @date 2021/12/24 17:07
 */
@Slf4j
@Service
public class OmRoleServiceImpl implements OmRoleService {

    @Resource
    private OmRoleDao omRoleDao;
    @Resource
    private OmUserDao omUserDao;

    @Override
    public ServiceResult<UserPageDTO<OmRoleVO>> pageRole(Integer page, Integer size) {
        int total = omRoleDao.count();
        List<OmRole> omRoles = omRoleDao.listRole(page, size);
        List<OmRoleVO> omRoleList = omRoles.stream()
                .map(OmRoleVO::convertToVO)
                .collect(Collectors.toList());

        return ServiceResult.success(new UserPageDTO<>(omRoleList, total, page));
    }

    @Override
    public ServiceResult<SimpleIdVO> updateRole(UpdateOmRoleParam param) {
        OmRole omRole = omRoleDao.findById(param.getId());
        if (Objects.isNull(omRole)) {
            return ServiceResult.failWithCode(ResourceErrorCodeEnum.NOT_FOUND);
        }
        if (StringUtils.equals(OmRoleTypeEnum.SUPER_ADMIN.getName(), omRole.getTitle())) {
            return ServiceResult.failWithCode(ResourceErrorCodeEnum.UPDATE_FORBID);
        }
        if (StringUtils.equals(OmRoleTypeEnum.SALE_LEADER.getName(), omRole.getTitle())) {
            param.setTitle(OmRoleTypeEnum.SALE_LEADER.getName());
        }
        return ServiceResult.success(SimpleIdVO.getInstance(omRoleDao.saveRole(UpdateOmRoleParam.convertToEntiy(param)).getId()));
    }

    @Override
    public ServiceResult<OmRoleVO> findById(String id) {
        OmRole omRole = omRoleDao.findById(id);
        if (Objects.isNull(omRole)) {
            return ServiceResult.failWithCode(ResourceErrorCodeEnum.NOT_FOUND);
        }
        OmRoleVO omRoleVO = OmRoleVO.convertToVO(omRole);
        return ServiceResult.success(omRoleVO);
    }

    @Override
    public ServiceResult<HashMap<String, Integer>> deleteById(String id) {
        OmUser omUser = omUserDao.findByRoleId(id);
        if (Objects.nonNull(omUser)) {
            return ServiceResult.failWithCode(ResourceErrorCodeEnum.DELETE_INHIBIT);
        }
        OmRole omRole = omRoleDao.findById(id);
        if (Objects.isNull(omRole)) {
            return ServiceResult.failWithCode(ResourceErrorCodeEnum.NOT_FOUND);
        }
        if (OmRoleTypeEnum.SUPER_ADMIN.getIndex().equals(omRole.getType())) {
            return ServiceResult.failWithCode(ResourceErrorCodeEnum.DATA_NOT_ALLOW_DELETE);
        }
        omRoleDao.deleteById(id);
        HashMap<String, Integer> map = new HashMap<>(16);
        map.put("deletedCount", 1);
        return ServiceResult.success(map);
    }

    @Override
    public ServiceResult<SimpleIdVO> create(CreateOmRoleParam createOmRoleParam) {
        OmRole dumplicatOmrole = omRoleDao.findByTitle(createOmRoleParam.getTitle());
        if (Objects.nonNull(dumplicatOmrole)) {
            return ServiceResult.failWithCode(ValidationErrorCodeEnum.NAME_DUPLICATE);
        }
        OmRole omRole = CreateOmRoleParam.convertToEntiy(createOmRoleParam);
        if (Objects.isNull(omRole)) {
            return ServiceResult.failWithCode(ReqErrorCodeEnum.PARAM_LOST);
        }
        omRole.setType(OmRoleTypeEnum.COMMON_USER.getIndex());
        return ServiceResult.success(SimpleIdVO.getInstance(omRoleDao.saveRole(omRole).getId()));
    }

    @Override
    public ServiceResult<List<com.bestcem.xm.om.grpc.v1.services.OmRole>> listRole(OmRole omRole) {
        List<OmRole> omRoles = omRoleDao.findByCondition(omRole);
        if (CollUtil.isEmpty(omRoles)) {
            return ServiceResult.fail("查询信息为空");
        }
        List<com.bestcem.xm.om.grpc.v1.services.OmRole> grpcRoles = omRoles.stream().map(OmUserConverter::convertToGrpcRole).collect(Collectors.toList());
        return ServiceResult.success(grpcRoles);
    }
}
